﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; 

namespace WorkFlowData
{
    /// <summary>
    /// 数据库查询类
    /// </summary>
    public partial class WorkFlowDataContext : System.Data.Linq.DataContext
    { 
        /////////////////////////////////////////////////////////////////////////////////////////////////////
        #region "Search"

        /// <summary> 
        /// 获取所有用户个人信息
        /// </summary>
        /// <param name="keys"></param>
        /// <returns></returns>
        public object userGetAllInfo(string keys)
        {
            var users = from u in tUsers
                        where keys.Equals(string.Empty) || keys.Split(';').Contains( u.id.ToString()) 
                        select new
                        {
                            u.id ,
                            u.tUsersDetails.name,
                            u.login,
                            u.password,
                            u.tUsersDetails.address,
                            u.tUsersDetails.company,
                            u.tUsersDetails.telephone, 
                            u.tUsersDetails.mobile, 
                            u.tUsersDetails.email, 
                            u.tUsersDetails.fax, 
                            groupName = u.tUserGroup.name
                        }; 
            return   users ;
        }

        /// <summary>
        /// 获取个人信息，用于修改个人信息页面
        /// </summary>
        /// <param name="keyU"></param>
        /// <returns></returns>
        public object userGetDetail(int keyU)
        {
            //TODO 完成该函数
            return null;
        }

        /// <summary>
        /// 获取用户名，密码 用于修改密码页面
        /// </summary>
        /// <param name="keyU"></param>
        /// <returns></returns>
        public object userGetLogin(int keyU)
        {
            //TODO 
            return null;
        }

        /// <summary> 
        /// 返回ID，用户名，密码列表
        /// </summary>
        /// <param name="keys"></param>
        /// <returns></returns>
        public object userGetShortList(string keys)
        {
            var users = from u in tUsers
                        where keys.Equals(string.Empty) || keys.Split(';').Contains(u.id.ToString()) 
                        select u;
            return users;
        }

        /// <summary>
        /// 返回用户组列表
        /// </summary>
        /// <returns></returns>
        public object userGroupGetList()
        {
            //TODO 完成函数
            var userGroup = from ug in tUserGroups
                        select ug;
            return userGroup;
        }
         
        /// <summary>
        /// 返回用户权限列表
        /// </summary>
        /// <returns></returns>
        public object permissionGetList()
        {  
            var userPermission = from up in tPermissions 
                            select up;
            return userPermission;
        }
         
        /// <summary>
        /// 登录判断函数，判断是否为正确的用户名密码
        /// </summary>
        /// <param name="login"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool  userIsRegistered(string login , string password )
        {
            var user = from u in tUsers 
                            where u.login == login  && u.password == password 
                            select u;
            if (user.Count() > 0)  
                return true; 
            else 
                return false; 
        }
         
        /// <summary>
        /// 判断用户名是否已经被注册
        /// </summary>
        /// <param name="login"></param>
        /// <returns></returns>
        public bool userNameIsRegistered(string login)
        {
            var user = from u in tUsers
                       where u.login == login  
                       select u;
            if (user.Count() > 0)
                return true;
            else
                return false;
        }
         
        #endregion
         
        /////////////////////////////////////////////////////////////////////////////////////////////////////
        #region "Insert"  
        /// <summary>
        /// 增加一个用户
        /// </summary>
        /// <param name="login"></param>
        /// <param name="password"></param>
        /// <param name="name"></param>
        /// <param name="telephone"></param>
        /// <param name="mobile"></param>
        /// <param name="fax"></param>
        /// <param name="email"></param>
        /// <param name="company"></param>
        /// <param name="address"></param>
        /// <returns></returns>
        public string userInsert(string login, string password, string name , string telephone , string mobile, string fax, string email , 
                            string company, string address)
        {
            try
            {
                tUser user = new tUser { login = login, password = password, addTime = DateTime.Now };
                user.tUsersDetails = new tUsersDetail { name = name, telephone = telephone, mobile = mobile, email = email, fax = fax, company = company, address = address };

                tUsers.InsertOnSubmit(user);
                this.SubmitChanges();
                return string.Empty ;
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
         
        /// <summary> 
        /// 增加一个用户组
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public string  userGroupInsert(string name)
        {
            try
            {
                tUserGroup ug = new tUserGroup { name = name };
                tUserGroups.InsertOnSubmit(ug);
                this.SubmitChanges();
                return string.Empty;
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }

         
        /// <summary>
        /// 权限录入函数
        /// </summary>
        /// <param name="name"></param>
        /// <param name="code"></param>
        /// <param name="description"></param>
        /// <returns></returns>
        public string  permissionInsert(string name, string code , string description )
        {
            try
            {
                tPermission p = new tPermission { name = name, code = code , description = description  };
                tPermissions.InsertOnSubmit(p);
                this.SubmitChanges();
                return string.Empty;
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }

        /// <summary>
        /// 增加用户权限对
        /// </summary>
        /// <param name="keyUser"></param>
        /// <param name="keyUserGroup"></param>
        /// <param name="keyPermission"></param>
        /// <returns></returns>
        public string  userPermissionInsert(int keyUser, int keyUserGroup , int keyPermission)
        {
            try
            {
                rUserPermission p = new rUserPermission { keyPermission = keyPermission, keyUser= keyUser , keyUserGroup = keyUserGroup };
                rUserPermissions.InsertOnSubmit(p);
                this.SubmitChanges();
                return string.Empty;
            }
            catch (Exception ex)
            {
                return ex.ToString()  ;
            }
        }
          
        #endregion
         
        /////////////////////////////////////////////////////////////////////////////////////////////////////
        #region "Update"
        /// <summary>
        /// 更新密码, 不允许更改用户名
        /// </summary>
        /// <param name="keyU"></param> 
        /// <param name="password"></param>
        /// <returns></returns>
        public string userUpdatePassword(int keyU,  string password)
        {
            return string.Empty;
        }

        /// <summary>
        /// 更新用户个人信息
        /// </summary>
        /// <param name="keyU"></param>
        /// <param name="name"></param>
        /// <param name="telephone"></param>
        /// <param name="mobile"></param>
        /// <param name="fax"></param>
        /// <param name="email"></param>
        /// <param name="company"></param>
        /// <param name="address"></param>
        /// <returns></returns>
        public string userDetailUpdate(int keyU, string name, string telephone, string mobile, string fax, string email,
                            string company, string address)
        {
            return string.Empty;
        }

        /// <summary>
        /// 更新用户组
        /// </summary>
        /// <param name="keyG"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public string userGroupUpdate(int keyG, string name )
        {
            return string.Empty;
        }

        /// <summary>
        /// 更新权限
        /// </summary>
        /// <param name="keyP"></param>
        /// <param name="name"></param>
        /// <param name="code"></param>
        /// <param name="description"></param>
        /// <returns></returns>
        public string permissionUpdatge(int keyP, string name, string code , string description)
        {
            return string.Empty;
        } 

        #endregion

        /////////////////////////////////////////////////////////////////////////////////////////////////////
        #region "delete" 
        /// <summary>
        /// 删除全部账户信息
        /// </summary>
        /// <param name="keyU"></param>
        /// <returns></returns>
        public string userDel(int keyU)
        {
            return string.Empty;
        }
           
        /// <summary>
        /// 在没有用户在该组下时，删除相应用户组
        /// </summary>
        /// <param name="keyG"></param>
        /// <returns></returns>
        public string userGroupDel(int keyG)
        {
            return string.Empty;
        }

        /// <summary>
        /// 删除相应权限，并删除相应用户的该权限联系
        /// </summary>
        /// <param name="keyP"></param>
        /// <returns></returns>
        public string permissionDel(int keyP )
        {
            return string.Empty;
        } 

        #endregion
    }
}
